package myfamily.model;

import java.io.IOException;
import java.io.InputStream;
import java.util.Collection;
import java.util.Properties;

import javax.jdo.JDOHelper;
import javax.jdo.PersistenceManagerFactory;
import javax.jdo.Query;

import myfamily.InsertData;

/**
 * @author Vineet
 *
 */
public class DatabaseObjectFactory {
	private static DatabaseObject db;

	public static DatabaseObject getDatabaseObject()
	{
		if(db != null )
			return db;
		else
		{
			db = new DatabaseObject();
			initializePM();
			return db;
		}
	}

	private static void initializePM()
	{
		try{
			// load the properties file to connect
			Properties p = loadProperties();
			// run against local or remote server
			PersistenceManagerFactory pmf = JDOHelper.getPersistenceManagerFactory(p);
			db.pm = pmf.getPersistenceManager();
		}
		catch(Exception e)
		{
			e.printStackTrace();
		}
	}


	private static Properties loadProperties() throws IOException {
		Properties p = new Properties();
		//InputStream in = null;
		try {
		/*in = InsertData.class.getResourceAsStream("/versant.properties");
			
			if (in == null) throw new IOException("versant.properties not on classpath");
			p.load(in);
			System.out.println(p.getProperty("javax.jdo.option.ConnectionURL"));*/
			
			
			p.setProperty("javax.jdo.PersistenceManagerFactoryClass","com.versant.core.jdo.BootstrapPMF");
			p.setProperty("javax.jdo.option.NontransactionalRead","true");
			p.setProperty("javax.jdo.option.ConnectionUserName","");
			p.setProperty("javax.jdo.option.ConnectionURL","myFamilyDB2@localhost:5019");
		    p.setProperty("versant.validateMappingOnStartup","false");
		    p.setProperty("versant.vdsSchemaEvolve","true");
		    p.setProperty("versant.remoteAccess","false");
		    p.setProperty("versant.metadata.0","myFamily.jdo");

		} finally {
			//if (in != null) in.close();
		}
		return p;
	}
}